/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techshop.dao.impl;

import com.techshop.dao.PhoneCodeDao;
import com.techshop.model.PhoneCode;
import java.util.List;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import redis.client.ValidatePhonePubSubClient;

/**
 *
 * @author Hai-PC
 */
public class PhoneCodeDaoImpl extends HibernateDaoSupport implements PhoneCodeDao
{
    @Override
    public PhoneCode getById(int phoneCodeId)
    {
        try
        {
            return (PhoneCode) getSession().get(PhoneCode.class, phoneCodeId);
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }
    
    @Override
    public PhoneCode getByPhoneAndColor(int phoneId, String colorId)
    {
        try
        {
            return (PhoneCode) getSession().createQuery("FROM PhoneCode WHERE phone.id=:phoneId AND color.id=:colorId").setInteger("phoneId", phoneId).setString("colorId", colorId).list().get(0);
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }
    
    @Override
    public List<PhoneCode> getByPhoneId(int phoneId)
    {
        try
        {
            return getSession().createQuery("FROM PhoneCode WHERE phone.id=:phoneId").setInteger("phoneId", phoneId).list();
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }
    
    @Override
    public boolean insert(PhoneCode phoneCode)
    {
        try
        {
            getHibernateTemplate().save(getHibernateTemplate().merge(phoneCode));
            ValidatePhonePubSubClient.updatePhone(phoneCode.getPhone().getId());
            return true;
        }
        catch (DataAccessResourceFailureException | DataIntegrityViolationException e)
        {
            System.err.printf("Error: ", e.toString());
            return true;
        }
    }
    
    @Override
    public boolean update(PhoneCode phoneCode)
    {
        try
        {
            if (getById(phoneCode.getId()) == null)
            {
                return false;
            }
            else
            {
                getHibernateTemplate().update(getHibernateTemplate().merge(phoneCode));
                ValidatePhonePubSubClient.updatePhone(phoneCode.getPhone().getId());
                return true;
            }
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public boolean delete(PhoneCode phoneCode)
    {
        try
        {
            if (getById(phoneCode.getId()) == null)
            {
                return false;
            }
            else
            {
                getHibernateTemplate().delete(getHibernateTemplate().merge(phoneCode));
                ValidatePhonePubSubClient.updatePhone(phoneCode.getPhone().getId());
                return true;
            }
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public List<PhoneCode> getAll()
    {
        try
        {
            return getSession().createQuery("FROM PhoneCode").list();
        }
        catch (DataAccessResourceFailureException | IndexOutOfBoundsException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public List<PhoneCode> search(String name)
    {
        try
        {
            return getSession().createQuery("FROM PhoneCode WHERE phone.name LIKE :name").setString("name", "%" + name + "%").list();
        }
        catch (DataAccessResourceFailureException | IndexOutOfBoundsException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }
}